package cn.ante.order.service.impl;

import cn.ante.order.domain.OrderPetAcquisition;
import cn.ante.order.dto.OrderComfirmDto;
import cn.ante.order.mapper.OrderPetAcquisitionMapper;
import cn.ante.order.service.IOrderPetAcquisitionService;
import cn.ante.basic.service.impl.BaseServiceImpl;
import cn.ante.pet.domain.Pet;
import cn.ante.pet.domain.SearchMasterMsg;
import cn.ante.pet.mapper.PetMapper;
import cn.ante.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-07-19
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Override
    public void confirmOrder(OrderComfirmDto orderComfirmDto) {
        // 收购订单id
        Long id = orderComfirmDto.getId();
        // 收购最终的价格，也是宠物的成本价
        BigDecimal money = orderComfirmDto.getMoney();
        // 收购时，员工使用的支付方式
        Integer payType = orderComfirmDto.getPayType();
        // 通过id拿到该订单的信息，当确认订单时，首先要将该订单的状态修改成已完成
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(1);
        // 将最终的收购金额修改
        orderPetAcquisition.setPrice(money);
        // 将员工的支付方式设置生成
        orderPetAcquisition.setPaytype(payType);
        // 最后调用修改方法
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 修改宠物信息中的成本价格
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);
        // 3.修改寻主消息状态为已完成
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    @Override
    public void cancelOrder(Long id) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        // 将收购订单的状态改成取消
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 将寻主任务的shopid置空，放回寻主池
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 删除宠物信息
        petMapper.remove(orderPetAcquisition.getPetId());
    }
}
